Polymer({
    //定义卡片id，该id在card.html的<dom-module>标签中被引用。
    is:"card-000000000000-100",
	properties: {

		//定义外部polymer属性变量，需带有“export:true”属性，部署安装时使用default值自动初始化。
		//外部变量的值可在学校管理平台“卡片管理”页面中修改，可直接在card.html页面中使用。
		card_title: {type: String, export:true, default: "卡片示例-今日天气"},
		thumbnail: {type: String, export:true, default: "thumbnail.png"},
		target_platform: {type: String, export:true, default: "WEB"},
		citykey: {type: String, export:true, default: "101190101"},

		//定义内部polymer属性变量，只能在ajax请求中初始化后，才可在card.html页面中使用。
		icon:{type:String},
		image: {type: String},
		temperature: {type: String},
		weather: {type: String},
		aqi: {type: String},
		high: {type: String},
		low: {type: String}
	},
    ready: function() {
        //定义全局card变量，获取appId,cardNumber。
        var card = this;
		var ids = card.is.split("-");
		var appId = ids[1];
		var cardNumber = ids[2];

		//使用外部polymer属性变量初始化内部变量的示例。
		card.icon = AmpUtils.getCardDeployPath(appId,cardNumber)+"resources/img/" +card.thumbnail;

		//请求数据和展现
        $.ajax( {
            type: 'get',

			//AmpUtils.getAppDeployPath(appId)是平台提供的方法，传入appId,用来获取该Widget所关联应用的部署根路径。
			//“/getJsonpData”是widget获取数据的相对路径，根据实际情况自行修改。
			url: AmpUtils.getAppDeployPath(appId)+card.citykey,

            dataType: "jsonp",
            jsonp: "callback",
            cache: false,

            success: function(data) {
            	var result = data.data;
            	var type = result.forecast[0].type;
            	var aqi = parseInt(result.aqi, 10);
            	
            	var image = AmpUtils.getCardDeployPath(appId,cardNumber)+"resources/img/";
            	switch (type) {
					case "晴":
						image += "sun.png";
						break;
					case "暴雨":
						image += "rainstorm.png";
						break;
					case "大雪":
						image += "heavySnow.png";
						break;
					case "小雪":
						image += "lightSnow.png";
						break;
					case "雨夹雪":
						image += "sleet.png";
						break;
					case "阴":
						image += "yin.png";
						break;
					case "大雨":
						image += "heavyRain.png";
						break;
					case "小雨":
						image += "lightRain.png";
						break;
					case "雷阵雨":
						image += "thunderShower.png";
						break;
					case "阵雨":
						image += "shower.png";
						break;
					case "雾":
						image += "fog.png";
						break;
					case "扬尘":
						image += "dust.png";
						break;
					case "多云":
						image += "cloudy.png";
						break;
					default :
						image += "null.png";
						break;
            	}
            	
            	var showAQI = "";
            	if(aqi >= 0 && aqi <= 50){
            		showAQI = aqi + " 空气优";
            	}else if(aqi >= 51 && aqi <= 100){
            		showAQI = aqi + " 空气良";
            	}else if(aqi >= 101 && aqi <= 150){
            		showAQI = aqi + " 空气轻度污染";
            	}else if(aqi >= 151 && aqi <= 200){
            		showAQI = aqi + " 空气中度污染";
            	}else if(aqi >= 201 && aqi <= 300){
            		showAQI = aqi + " 空气重度污染";
            	}else{
            		showAQI = aqi + " 空气严重污染";
            	}
            	
            	card.city = result.city;
				card.temperature = result.wendu+"°";
				card.weather = type;
				card.aqi = showAQI;
				card.image = image;
				card.high = result.forecast[0].high.replace("高温 ","").replace("℃","°");
				card.low = result.forecast[0].low.replace("低温 ","").replace("℃","°");
				card.fire("widgetComplete");
            },
            error: function(err){
            }
        });
    }
});
